import request from './index'

// 文件信息
export interface FileInfo {
  id: number
  fileName: string
  fileSize: number
  fileType: string
  isFolder: number
  parentId: number
  parentPath?: string
  status: number
  ossKey?: string
  createTime: string
  updateTime: string
}

// 文件列表查询参数
export interface FileListParams {
  parentId?: number
  page?: number
  size?: number
  orderBy?: string
  orderType?: string
}

// 文件列表响应
export interface FileListResponse {
  total: number
  page: number
  size: number
  content: FileInfo[]
}

// 上传签名信息
export interface UploadSignature {
  accessid: string
  policy: string
  signature: string
  dir: string
  host: string
  key: string
  expire: number
}

// 存储信息
export interface StorageInfo {
  totalStorage: number
  usedStorage: number
  availableStorage: number
  usagePercentage: number
}

/**
 * 获取文件列表
 * @param params 查询参数
 * @returns 文件列表
 */
export const getFileList = (params: FileListParams): Promise<FileListResponse> => {
  return request.get('/file/list', { params })
}

/**
 * 获取上传签名
 * @param params 上传参数
 * @returns 上传签名
 */
export const getUploadSignature = (params: {
  fileName: string
  fileSize: number
  contentType: string
  parentId: number
}): Promise<UploadSignature> => {
  return request.post('/file/upload-signature', null, { params })
}

/**
 * 保存文件元数据
 * @param params 文件元数据
 * @returns 保存结果
 */
export const saveFileMetadata = (params: {
  fileName: string
  fileSize: number
  fileType: string
  parentId: number
  ossKey: string
}): Promise<FileInfo> => {
  return request.post('/file/save-metadata', null, { params })
}

/**
 * 创建文件夹
 * @param params 文件夹信息
 * @returns 创建结果
 */
export const createFolder = (params: {
  folderName: string
  parentId: number
}): Promise<FileInfo> => {
  return request.post('/file/create-folder', null, { params })
}

/**
 * 删除文件
 * @param fileId 文件ID
 * @returns 删除结果
 */
export const deleteFile = (fileId: number): Promise<any> => {
  return request.post(`/file/delete/${fileId}`)
}

/**
 * 重命名文件
 * @param fileId 文件ID
 * @param newName 新名称
 * @returns 重命名结果
 */
export const renameFile = (fileId: number, newName: string): Promise<FileInfo> => {
  return request.post(`/file/rename/${fileId}`, null, { params: { newName } })
}

/**
 * 移动文件
 * @param fileId 文件ID
 * @param targetParentId 目标父级ID
 * @returns 移动结果
 */
export const moveFile = (fileId: number, targetParentId: number): Promise<FileInfo> => {
  return request.post(`/file/move/${fileId}`, null, { params: { targetParentId } })
}

/**
 * 获取存储信息
 * @returns 存储信息
 */
export const getStorageInfo = (): Promise<StorageInfo> => {
  return request.get('/file/storage-info')
}

/**
 * 生成文件下载链接
 * @param fileId 文件ID
 * @returns 下载链接信息
 */
export const generateDownloadUrl = (fileId: number): Promise<{
  downloadUrl: string
  fileName: string
  fileSize: number
  expireTime: number
}> => {
  return request.get(`/file/download/${fileId}`)
}

/**
 * 生成文件预览链接
 * @param fileId 文件ID
 * @returns 预览链接信息
 */
export const generatePreviewUrl = (fileId: number): Promise<{
  previewUrl: string
  fileName: string
  fileType: string
  fileSize: number
  supportPreview: boolean
  expireTime: number
}> => {
  return request.get(`/file/preview/${fileId}`)
}

/**
 * 获取回收站文件列表
 * @param params 查询参数 - 接口路径：GET /api/file/recycle/list，参数：page,size,orderBy,orderType
 * @returns 回收站文件列表
 */
export const getRecycleFileList = (params: {
  page?: number
  size?: number
  orderBy?: string
  orderType?: string
}): Promise<{
  content: FileInfo[]
  total: number
  page: number
  size: number
}> => {
  return request.get('/file/recycle/list', { params })
}

/**
 * 恢复回收站文件
 * @param fileId 文件ID - 接口路径：POST /api/file/recycle/restore/{fileId}
 * @returns 恢复后的文件信息
 */
export const restoreFile = (fileId: number): Promise<FileInfo> => {
  return request.post(`/file/recycle/restore/${fileId}`)
}

/**
 * 彻底删除文件
 * @param fileId 文件ID - 接口路径：POST /api/file/recycle/delete/{fileId}
 * @returns 删除结果
 */
export const permanentDeleteFile = (fileId: number): Promise<any> => {
  return request.post(`/file/recycle/delete/${fileId}`)
}

/**
 * 清空回收站
 * @returns 清空结果 - 接口路径：POST /api/file/recycle/clear
 */
export const clearRecycleBin = (): Promise<any> => {
  return request.post('/file/recycle/clear')
}